﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "QtGnufPropertyPallet.h"
#include "GnufPropertyPallet.h"
#include "JournalPropertyPalletRecorderAndReplayerCreator.h"
#include "GbmpNew.h"
#include "IParameter.h"
#include <QHBoxLayout>
#include "GnufQtToolbar.h"
#include "GnufAppearanceSetting.h"
#include <QLabel>
#include <QLineEdit>
#include "GnufInfo.h"
#include <QKeyEvent>
#include <QApplication>
#include "GnufFamilyTypes.h"
#include "GnufNameValuePairs.h"
#include "GnufPallet.h"
#include "NameHelper.h"
#include "UiCommonDialog.h"
#include "GnufWorkBench.h"
#include "GnufStyleSheets.h"
#include "GnufStyleSheetComposer.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gnuf;

namespace {
    const int PALLET_SPACE = 35;
    constexpr int s_familyNameLineEditHeight = 22;
}

gfam::QtGnufPropertyPallet::QtGnufPropertyPallet(GnufPropertyPallet* gnufPropertyPallet)
    : QWidget(nullptr)
    , m_gnufPropertyPallet(gnufPropertyPallet)
    , m_familyTypes(gcmp::UniqueString::Find("FamilyTypes"), gnufPropertyPallet->GetFamilyTypeComponentBuilder(), QtGnufToolbar::eFamilyType)
    , m_nameValuePairs(gcmp::UniqueString::Find("PropertyList"), gnufPropertyPallet->GetInstanceNameValuePairsBuilder(), QtGnufToolbar::eNameValuePairs)
{
    this->setObjectName(QString("PropertyPalette"));

    // 注册日志自定义录制回放接口
    gfam::JournalPropertyPalletRecorderAndReplayerCreator::RegisterToJournal(this);

    QVBoxLayout* vlayout = NEW_AS_QT_LAYOUT(QVBoxLayout, this);
    DBG_WARN_AND_RETURN_VOID_UNLESS(vlayout, L"vlayout为空!", L"paramodel", L"2024-02-20");
    setLayout(vlayout);
    vlayout->setContentsMargins(0, 0, 0, 0);
    vlayout->setSpacing(0);
    vlayout->addWidget(m_familyTypes.qtToolbar(), 0, Qt::AlignTop);
    vlayout->addWidget(m_nameValuePairs.qtToolbar(), 0, Qt::AlignTop);
    vlayout->addStretch();

    DBG_WARN_AND_RETURN_VOID_UNLESS(m_nameValuePairs.qtToolbar(), L"m_nameValuePairs.qtToolbar()为空!", L"paramodel", L"2024-02-20");
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_nameValuePairs.qtToolbar()->item(), L"m_nameValuePairs.qtToolbar()->item()为空!", L"paramodel", L"2024-02-20");
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_nameValuePairs.qtToolbar()->item()->appearanceSetting(), L"m_nameValuePairs.qtToolbar()->item()->appearanceSetting()为空!", L"paramodel", L"2024-02-20");
    const gnuf::NameValuePairsAppearanceSetting& setting = m_nameValuePairs.qtToolbar()->item()->appearanceSetting()->nameValuePairsAppearanceSetting();
   
    QHBoxLayout* hLayout = NEW_AS_QT_LAYOUT(QHBoxLayout);
    DBG_WARN_AND_RETURN_VOID_UNLESS(hLayout, L"hLayout为空!", L"paramodel", L"2024-02-20");
    hLayout->setContentsMargins(0, 0, setting.titleMargin(), setting.titleMargin());

    m_qtApply = NEW_AS_QT_CHILD(QPushButton, QString::fromStdWString(L"应用"), this);
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_qtApply, L"m_qtApply为空!", L"paramodel", L"2024-02-20");
    m_qtApply->setObjectName("PropertyWidgetApplyButton");
    m_qtApply->setStyleSheet(applyBtnStyleSheet());
    m_qtApply->setEnabled(false);

    connect(m_qtApply, &QPushButton::clicked, this, &QtGnufPropertyPallet::onApply);
    hLayout->addWidget(m_qtApply, 0, Qt::AlignRight);
    vlayout->addLayout(hLayout);

    m_nameValuePairs.qtToolbar()->setFocusPolicy(Qt::StrongFocus);
    m_nameValuePairs.qtToolbar()->installEventFilter(this);
}

gfam::QtGnufPropertyPallet::~QtGnufPropertyPallet()
{

}

void gfam::QtGnufPropertyPallet::destroy()
{
    m_familyTypes.destroy();
    m_nameValuePairs.destroy();

    // Finally, delete me, later.
    deleteLater();
}

void gfam::QtGnufPropertyPallet::setEnabled(bool enable)
{
    QWidget::setEnabled(enable);
}

bool gfam::QtGnufPropertyPallet::isPropertyValueChanged() const
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_qtApply, L"m_qtApply为空指针", L"paramodel", L"2024-02-20");
    return m_qtApply->isEnabled();
}

void gfam::QtGnufPropertyPallet::onPropertyValueChanged()
{
    //当属性值发生改变时发出信号
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_qtApply, L"m_qtApply为空指针", L"paramodel", L"2024-02-20");
    m_qtApply->setEnabled(true);
}

void gfam::QtGnufPropertyPallet::onApplyStatus()
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_qtApply, L"m_qtApply为空指针", L"paramodel", L"2024-02-20");
    if (m_qtApply->isEnabled())
    {
        m_qtApply->setEnabled(false);
    }
}

void gfam::QtGnufPropertyPallet::refreshAppearance()
{
    m_familyTypes.refreshAppearance();
    m_nameValuePairs.refreshAppearance();
}

void gfam::QtGnufPropertyPallet::resizeEvent(QResizeEvent * event)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(event, L"event为空指针", L"paramodel", L"2024-02-20");
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_familyTypes.qtToolbar(), L"m_familyTypes.qtToolbar()为空指针", L"paramodel", L"2024-02-20");

    int height = event->size().height() - m_familyTypes.qtToolbar()->height();
    height -= PALLET_SPACE;
    m_nameValuePairs.qtToolbar()->setFixedSize(event->size().width(), height);
}

void gfam::QtGnufPropertyPallet::leaveEvent(QEvent * event)
{
    if (isMouseLeavePropertyPallet())
    {
        takeLastChangedEffect();
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_gnufPropertyPallet, L"m_gnufPropertyPallet为空指针", L"paramodel", L"2024-02-20");
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_gnufPropertyPallet->GetInstanceNameValuePairsBuilder(), L"m_gnufPropertyPallet->GetInstanceNameValuePairsBuilder()为空指针", L"paramodel", L"2024-02-20");
        m_gnufPropertyPallet->GetInstanceNameValuePairsBuilder()->onApply();
    }
}

bool gfam::QtGnufPropertyPallet::eventFilter(QObject * obj, QEvent * ev)
{
    if (obj == m_nameValuePairs.qtToolbar() && ev->type() == QEvent::KeyPress)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
        if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
        {
            onApply();
            return true;
        }
    }
    return QWidget::eventFilter(obj, ev);
}

bool gfam::QtGnufPropertyPallet::takeLastChangedEffect()
{
    if (QLineEdit* focusLineEdit = dynamic_cast<QLineEdit*>(QApplication::focusWidget()))
    {
        focusLineEdit->clearFocus();
        return true;
    }
    return false;
}

QString gfam::QtGnufPropertyPallet::applyBtnStyleSheet()
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(m_familyTypes.qtToolbar(), L"m_familyTypes.qtToolbar()为空!", L"paramodel", L"2024-02-20");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(m_familyTypes.qtToolbar()->item(), L"m_familyTypes.qtToolbar()->item()为空!", L"paramodel", L"2024-02-20");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(m_familyTypes.qtToolbar()->item()->appearanceSetting(), L"m_familyTypes.qtToolbar()->item()->appearanceSetting()为空!", L"paramodel", L"2024-02-20");

    const gnuf::FamilyTypesAppearanceSetting* setting = m_familyTypes.qtToolbar()->item()->appearanceSetting()->familyTypesAppearanceSetting();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(setting, L"setting为空!", L"paramodel", L"2024-02-20");

    const gnuf::ButtonAppearanceSetting* btnSetting = setting->addFamilyTypeButtonAppearanceSetting();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(btnSetting, L"btnSetting为空!", L"paramodel", L"2024-02-20");

    //Normal
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(btnSetting->borderColor(), L"btnSetting->borderColor()为空!", L"paramodel", L"2024-02-20");
    QString borderColor = QString::fromStdString(btnSetting->borderColor()->rgba());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(btnSetting->backgroundColor(), L"btnSetting->backgroundColor()为空!", L"paramodel", L"2024-02-20");
    QString bgColor = QString::fromStdString(btnSetting->backgroundColor()->rgba());
    //Hover
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(btnSetting->hoverColor(), L"btnSetting->hoverColor()为空!", L"paramodel", L"2024-02-20");
    QString hBgColor = QString::fromStdString(btnSetting->hoverColor()->rgba());
    //Pressed
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(btnSetting->pressedColor(), L"btnSetting->pressedColor()为空!", L"paramodel", L"2024-02-20");
    QString pBgColor = QString::fromStdString(btnSetting->pressedColor()->rgba());
    //radius
    QString radius = QString::number(btnSetting->borderRadius());

    QString strStyleSheet = QString("QPushButton {\n"
        "border:1px solid;\n"
        "border-radius:%1;\n"
        "border-color:%2;\n"
        "background-color:%3;\n"
        "min-width:70;\n "
        "max-width:90;\n"
        "min-height:22;\n"
        "max-height:22;\n"
        "}\n"
        "\n"
        "QPushButton:hover {\n"
        "background-color:%4;\n"
        "}\n"
        "\n"
        "QPushButton:pressed {\n"
        "background-color:%5;\n"
        "}\n"
        "\n"
        "QPushButton:disabled {\n"
        "background-color:rgb(240,240,240);\n"
        "}\n"
        "\n"
    ).arg(radius, borderColor, bgColor, hBgColor, pBgColor);

    return strStyleSheet;
}

bool gfam::QtGnufPropertyPallet::isMouseLeavePropertyPallet() const
{
    // widget coordinate
    QRect rect(0, 0, this->width(), this->height());
    // the global screen coordinate pos to widget coordinates.
    QPoint cursorPoint = this->mapFromGlobal(QCursor::pos());
    if (rect.contains(cursorPoint))
        return false;
    return true;
}

void gfam::QtGnufPropertyPallet::onApply()
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_gnufPropertyPallet, L"m_gnufPropertyPallet为空!", L"paramodel", L"2024-02-20");
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_gnufPropertyPallet->GetInstanceNameValuePairsBuilder(), L"m_gnufPropertyPallet->GetInstanceNameValuePairsBuilder()为空!", L"paramodel", L"2024-02-20");
    m_gnufPropertyPallet->GetInstanceNameValuePairsBuilder()->onApply();
}

gfam::QtNameFamilyType::QtNameFamilyType(FamilyTypeNameComponent* pComponentBuilder, const std::wstring& name, bool wantToRename)
    : m_familyTypeNameComponent(pComponentBuilder)
    , m_qtName(nullptr)
    , m_qtRename(nullptr)
{
    this->setObjectName(QString("QtNameFamilyType"));
    QHBoxLayout* hlayout = NEW_AS_QT(QHBoxLayout, this);
    DBG_WARN_AND_RETURN_VOID_UNLESS(hlayout, L"hlayout为空!", L"paramodel", L"2024-02-20");
    setLayout(hlayout);

    QLabel* qtLabel = NEW_AS_QT(QLabel, this);
    DBG_WARN_AND_RETURN_VOID_UNLESS(qtLabel, L"qtLabel为空!", L"paramodel", L"2024-02-20");
    qtLabel->setText(QString::fromStdWString(GBMP_TR(L"名称:")));
    hlayout->addWidget(qtLabel);

    m_qtName = NEW_AS_QT(QLineEdit, this);
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_qtName, L"m_qtName为空!", L"paramodel", L"2024-02-20");
    m_qtName->setObjectName(QString("FamilyTypeNameLineEdit"));
    m_qtName->setText(QString::fromStdWString(name));
    hlayout->addWidget(m_qtName);

    if (wantToRename)
    {
        this->setObjectName(QString("QtNameFamilyTypeRename"));
        m_qtName->setReadOnly(true);
        m_qtName->setStyleSheet(" QLineEdit { background-color: grey } ");
        
        m_qtRename = NEW_AS_QT(QPushButton, this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_qtRename, L"m_qtRename为空!", L"paramodel", L"2024-02-20");
        m_qtRename->setObjectName(QString("RenameButton"));
        m_qtRename->setFocusPolicy(Qt::NoFocus);
        m_qtRename->setText(QString::fromStdWString(GBMP_TR(L"重命名")));
        hlayout->addWidget(m_qtRename);

        connect(m_qtRename, &QPushButton::clicked, this, [this](bool)->void
        {
            QtNameFamilyType rename(this->m_familyTypeNameComponent, this->m_qtName->text().toStdWString(), false);
            if (0 == rename.popup(GBMP_TR(L"重命名"))) {
                this->m_qtName->setText(QString::fromStdWString(rename.name()));
            }
        });
    }
}

gfam::QtNameFamilyType::~QtNameFamilyType()
{
}

int gfam::QtNameFamilyType::popup(const std::wstring & caption)
{
    gnuf::Pallet pallet;
    pallet.rHeader().caption = caption;
    pallet.rFooter().buttons = {
        GBMP_TR(L"确定"), // OK button
        GBMP_TR(L"取消")  // Cancel button
    };
    pallet.rFooter().currentButton = 0;
    pallet.rHeader().addButton(NEW_AS_OWNER_PTR(gnuf::Info, GBMP_TR(L"关闭"), GBMP_TR(L"关闭"), L":/images/Close"));
    pallet.setQtContent(this);
    pallet.setEventHander(this);
    pallet.setWidth(360);
    pallet.setHeight(136);
    m_pallet = nullptr;
    // Disconnect from the pallet whose lift time is going to terminate.
    setParent(nullptr);

    return pallet.footer().currentButton;
}

void gfam::QtNameFamilyType::setName(const std::wstring & name)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_qtName, L"m_qtName无效", L"paramodel", L"2024-02-20");
    m_qtName->setText(QString::fromStdWString(name));
}

std::wstring gfam::QtNameFamilyType::name() const
{
    DBG_WARN_AND_RETURN_UNLESS(m_qtName, L" ", L"m_qtName无效", L"paramodel", L"2024-02-20");
    return m_qtName->text().toStdWString();
}

void gfam::QtNameFamilyType::refresh()
{
    setStyle();
}

void gfam::QtNameFamilyType::setStyle()
{
    if (m_qtName)
    {
        m_qtName->setStyleSheet(
            "QLineEdit{color:" + QString::fromStdString(Color::fromCode(ColorCode::Text01).hex()) + ";}"
            "QLineEdit{background-color:" + QString::fromStdString(Color::fromCode(ColorCode::Input01).hex()) + ";}"
            "QLineEdit:read-only{background-color:" + QString::fromStdString(Color::fromCode(ColorCode::PanelM).hex()) + ";}"
            "QLineEdit{border: 1px solid" + QString::fromStdString(Color::fromCode(ColorCode::Line01).hex()) + ";}"
            "QLineEdit:hover:!read-only {border: 1px solid" + QString::fromStdString(Color::fromCode(ColorCode::B02).hex()) + ";}"
            "QLineEdit:focus:!read-only {border: 1px solid" + QString::fromStdString(Color::fromCode(ColorCode::B02).hex()) + ";}"
        );
        m_qtName->setFixedHeight(s_familyNameLineEditHeight);
    }
    if (m_qtRename != nullptr)
    {
        if (WorkBench * wb = WorkBench::get())
        {
            if (const StyleSheetComposer * ssc = wb->styleSheetComposer())
            {
                const StyleSheet* pButton = ssc->styleSheetBy(StyleSheet::eToQPushButton);
                if (const StyleSheetToQPushButton* pButtonStyleSheet = pButton->constCast<StyleSheetToQPushButton>())
                {
                    m_qtRename->setStyleSheet(QString::fromStdString(pButtonStyleSheet->toString()));
                }
            }
        }
    }
}

bool gfam::QtNameFamilyType::onFooterButtonClicked(gnuf::Pallet * pallet)
{
    if (nullptr == pallet)
    {
        return false;
    }

    if (0 == pallet->footer().currentButton) // OK button
    {
        DBG_WARN_AND_RETURN_FALSE_UNLESS(m_qtName, L"m_qtName无效", L"paramodel", L"2024-02-20");
        DBG_WARN_AND_RETURN_FALSE_UNLESS(m_familyTypeNameComponent, L"m_gnufPropertyPallet无效", L"paramodel", L"2024-02-20");
        const std::wstring text(m_qtName->text().toStdWString());
        if (!NameHelper::CheckFamilyTypeName(text))
        {
            gcmp::UiCommonDialog::ShowMessageBox(GBMP_TR(L"错误"), GBMP_TR(L"参数名称不合理!"), (int)gcmp::UiCommonDialog::ButtonType::OK);
            return nullptr;
        }
        if (m_familyTypeNameComponent->validateFamilyTypeName(text)) {
            m_familyTypeNameComponent->onFamilyTypeNameChanged(text);
            return true;
        }
        else {
            // Return true not until valid or Cancel button clicked.
            return false;
        }
    }
    else if (1 == pallet->footer().currentButton) // Cancel button
    {
        return true;
    }

    return false;
}

void gfam::QtNameFamilyType::onHeaderButtonClicked(gnuf::Pallet * pPallet)
{
    if (pPallet)
        pPallet->hide(false);
}

void gfam::QtNameFamilyType::setRenameEnabled(bool enable)
{
    if (m_qtRename)
        m_qtRename->setEnabled(enable);
}

//_____________________________________________________________________________
// QtEditFamilyType class implementation
//
gfam::QtEditFamilyType::QtEditFamilyType(
    FamilyNameValuePairsComponent* pFamilyNameValuePairsBuilder,
    const std::wstring& name)
    : m_familyNameValuePairsBuilder(pFamilyNameValuePairsBuilder)
    , m_qtName(nullptr)
    , m_mainLayout(nullptr)
    , m_previewGeometry(nullptr)
    , m_nameValuePairs(gcmp::UniqueString::Find("EditFamilyType"), pFamilyNameValuePairsBuilder, QtGnufToolbar::eNameValuePairs)
    , m_previewType(ePreviewType::eNone)
{
    this->setObjectName(QString("QtEditFamilyType"));
    m_mainLayout = NEW_AS_QT_LAYOUT(QHBoxLayout, this);
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_mainLayout, L"m_mainLayout为空!", L"paramodel", L"2024-02-20");
    setLayout(m_mainLayout);
    setContentsMargins(0, 0, 0, 0);

    QVBoxLayout* vlayout = NEW_AS_QT_LAYOUT(QVBoxLayout, this);
    DBG_WARN_AND_RETURN_VOID_UNLESS(vlayout, L"vlayout为空!", L"paramodel", L"2024-02-20");
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_familyNameValuePairsBuilder, L"m_familyNameValuePairsBuilder为空!", L"paramodel", L"2024-02-20");
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_familyNameValuePairsBuilder->GetFamilyTypeNameComponent(), L"m_familyNameValuePairsBuilder->GetFamilyTypeNameComponent()为空!", L"paramodel", L"2024-02-20");
    m_qtName = m_familyNameValuePairsBuilder->GetFamilyTypeNameComponent()->GetQtNameFamilyType();
    vlayout->addWidget(m_qtName, 0, Qt::AlignTop);
    vlayout->addWidget(m_nameValuePairs.qtToolbar(), 0, Qt::AlignTop);
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_nameValuePairs.qtToolbar(), L"m_nameValuePairs.qtToolbar()为空!", L"paramodel", L"2024-02-20");
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_nameValuePairs.qtToolbar()->item(), L"m_nameValuePairs.qtToolbar()->item()空!", L"paramodel", L"2024-02-20");
    m_nameValuePairs.qtToolbar()->item()->setUserData(gcmp::Any(EDIT_TYPE_NUM));
    vlayout->addStretch();
    m_mainLayout->addLayout(vlayout);
}

gfam::QtEditFamilyType::~QtEditFamilyType()
{
    m_nameValuePairs.destroy();
}

QLabel * gfam::QtEditFamilyType::previewGeometry()
{
    if (!m_previewGeometry)
    {
        m_previewGeometry = NEW_AS_QT_CHILD(QLabel, this);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(m_mainLayout, L"m_mainLayout为空!", L"paramodel", L"2024-02-20");
        m_mainLayout->addWidget(m_previewGeometry);
    }
    return m_previewGeometry;
}

void gfam::QtEditFamilyType::refresh()
{
    if (m_qtName != nullptr)
    {
        m_qtName->refresh();
    }
    m_nameValuePairs.refreshAppearance();
}

int gfam::QtEditFamilyType::popup(const std::wstring & caption, QWidget* parent /* = nullptr */, bool canPreview /* = false */, ePreviewType eType /* = ePreviewType::eNone */)
{
    gnuf::Pallet pallet;
    pallet.rHeader().caption = caption;
    pallet.rFooter().currentButton = 0;
    pallet.rHeader().addButton(NEW_AS_OWNER_PTR(gnuf::Info, GBMP_TR(L"关闭"), GBMP_TR(L"关闭"), L":/images/Close"));
    if (canPreview) {
        //1. add one command button
        pallet.rFooter().leftItems = {
            { gnuf::PalletFooter::LeftItem::Type::eButton, GBMP_TR(L"确定") },
            { gnuf::PalletFooter::LeftItem::Type::eButton, GBMP_TR(L"取消") },
            { gnuf::PalletFooter::LeftItem::Type::eButton, GBMP_TR(L"参数图示") } };
        //2.add geometry to show picture
        if (!m_previewGeometry) {
            if (m_previewGeometry = NEW_AS_QT_CHILD(QLabel, this))
            {
                m_previewGeometry->setObjectName(QString::fromStdWString(GBMP_TR(L"ParameterPreviewGeometry")));
                DBG_WARN_AND_RETURN_UNLESS(m_mainLayout, -1, L"m_mainLayout为空!", L"paramodel", L"2024-02-20");
                m_mainLayout->addWidget(m_previewGeometry);
            }
        }
        //3.get previewType, in order to resize this
        m_previewType = eType;
    }
    else {
        pallet.rFooter().buttons = {
            GBMP_TR(L"确定"), // OK button
            GBMP_TR(L"取消")  // Cancel button
        };
        if (m_previewGeometry) {
            m_previewGeometry->hide();
        }
    }
    m_szResized = QSize(336, 408);
    pallet.setQtContent(this);
    pallet.setEventHander(this);
    DBG_WARN_AND_RETURN_UNLESS(m_qtName, -1, L"m_qtName为空!", L"paramodel", L"2024-02-20");
    m_qtName->setFixedSize(336, 56);
    DBG_WARN_AND_RETURN_UNLESS(m_nameValuePairs.qtToolbar(), -1, L"m_nameValuePairs.qtToolbar()为空!", L"paramodel", L"2024-02-20");
    m_nameValuePairs.qtToolbar()->setFixedSize(336, 302);
    //size
    resize(m_szResized);
    //pos
    if (parent) {
        pallet.setX((parent->width() - this->width()) / 2);
        pallet.setY((parent->height() - this->height()) / 2);
    }
    //show
    pallet.show(true);

    // Disconnect from the pallet whose lift time is going to terminate.
    setParent(nullptr);

    return pallet.footer().currentButton;
}

bool gfam::QtEditFamilyType::onFooterButtonClicked(gnuf::Pallet* pallet)
{
    if (nullptr == pallet)
    {
        return false;
    }

    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_familyNameValuePairsBuilder, L"m_familyNameValuePairsBuilder为空!", L"paramodel", L"2024-02-20");
    if (0 == pallet->footer().currentButton) // OK button
    {
        m_familyNameValuePairsBuilder->onEditTypeDialogOkBtnClicked();
        return true;
    }
    else if (1 == pallet->footer().currentButton) // Cancel button
    {
        m_familyNameValuePairsBuilder->onEditTypeDialogCancelClicked();
        return true;
    }
    else if (2 == pallet->footer().currentButton) // Parameter Preview button
    {
        switch (m_previewType) {
        case ePreviewType::eStandard:
            m_szResized = QSize(666, 408);
            break;
        case ePreviewType::eLong:
            m_szResized = QSize(666, 568);
            break;
        case ePreviewType::eWide:
            m_szResized = QSize(826, 408);
            break;
        default:
            m_szResized = QSize(336, 408);
            break;
        }

        if (pallet->qtWidget()) {
            DBG_WARN_AND_RETURN_FALSE_UNLESS(m_previewGeometry, L"m_previewGeometry为空!", L"paramodel", L"2024-02-20");
            DBG_WARN_AND_RETURN_FALSE_UNLESS(pallet->qtWidget(), L"pallet->qtWidget()为空!", L"paramodel", L"2024-02-20");
            if (m_previewGeometry->isVisible()) {
                pallet->qtWidget()->resize(QSize(336, 408));
                m_previewGeometry->hide();
            }
            else {
                //m_familyNameValuePairsBuilder->onEditTypeDialogPreviewBtnClicked();
                pallet->qtWidget()->resize(m_szResized);
                m_previewGeometry->show();
            }
        }
        return false;
    }
    return false;
}

void gfam::QtEditFamilyType::setName(const std::wstring& name)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_qtName, L"m_qtName为空!", L"paramodel", L"2024-02-20");
    m_qtName->setName(name);
}

void gfam::QtEditFamilyType::onHeaderButtonClicked(gnuf::Pallet* pPallet)
{
    if (pPallet)
        pPallet->hide(false);
}
